package com.jing.controller;

import com.alibaba.fastjson.JSONObject;
import com.jing.common.core.domain.R;
import com.jing.common.core.web.controller.BaseController;
import com.jing.domain.Vo.ShopAttributeVO;
import com.jing.domain.Vo.ShopSkuStockVO;
import com.jing.domain.dto.GoodsInfoDTO;
import com.jing.domain.dto.Linked;
import com.jing.domain.dto.ShopSkuStockDTO;
import com.jing.domain.po.Product;
import com.jing.domain.po.ProductAttributeValue;
import com.jing.domain.po.ShopAttribute;
import com.jing.service.PmsBrandSservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：wp
 * @Package：com.jing.controller
 * @Project：jing
 * @name：ProductController
 * @Date：2024/5/29 15:22
 */
@RestController
@Slf4j
@Api(tags = "商品添加")
@RequestMapping("PmsBrand")
public class ProductController extends BaseController {


    @Autowired
    private PmsBrandSservice pmsBrandService;

    @ApiOperation("商品添加")
    @PostMapping("add")
    public R add(@RequestBody Product product) {
        log.info ( "=================={}========================", product );
        return R.ok ();
    }


    @ApiOperation("下拉框查询")
    @GetMapping("pband")
    public R<List<ProductAttributeValue>> listpb() {
        List<ProductAttributeValue> list = pmsBrandService.list ();
        return R.ok ( list );
    }

    @ApiOperation("子查询")
    @GetMapping("pba")
    public R<List<ShopAttributeVO>> lisasd(@RequestParam(required = false) Integer id
            , @RequestParam(required = false) Integer type) {
        List<ShopAttributeVO> list = pmsBrandService.lisasd ( id, type );
        return R.ok ( list );
    }

    @ApiOperation("商品查询")
    @GetMapping("product")
    public R<List<Product>> Productlist() {
        R<List<Product>> productlist = pmsBrandService.Productlist ();
        return productlist;
    }

    @ApiOperation("sku")
    @PostMapping("lisshop")
    public R<List<Map<String, String>>> list(@RequestBody List<Linked> linkeds) {
        // 使用流操作和Collectors.groupingBy将数据按cid分组
        Map<String, List<Linked>> groupedByCid = linkeds.stream()
                .collect(Collectors.groupingBy(
                        map -> String.valueOf(map.getId()), // 分组键
                        Collectors.toList() // 收集器
                ));
        //TODO  转换
        // 假设已经有了一个包含所有不同cid的Set
        Set<Integer> Cids = new HashSet<>(groupedByCid.keySet().stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList()));
        // 获取所有可能的组合
        List<Map<String, String>> com = new ArrayList<>();
        List<List<String>> cidValues = Cids.stream()
                .map(cid -> groupedByCid.getOrDefault(String.valueOf(cid), Collections.emptyList())
                        .stream()
                        .map(Linked::getName)
                        .collect(Collectors.toList()))
                .collect(Collectors.toList());

        // 使用笛卡尔积生成所有组合
        cartesianProduct(cidValues).forEach(combination -> {
            Map<String, String> result = new LinkedHashMap<> ();
            for (int i = 0; i < combination.size(); i++) {
                result.put(String.valueOf(Cids.toArray(new Integer[0])[i]), combination.get(i));
            }
            com.add(result);
        });
        // 输出结果
        System.out.println(JSONObject.toJSONString(com));

          return R.ok (com);
    }
    // 计算笛卡尔积的辅助方法
    private  List<List<String>> cartesianProduct(List<List<String>> lists) {
        if (lists == null || lists.isEmpty ()) {
            return Collections.emptyList ();
        }
        List<List<String>> result = new ArrayList<> ();
        if (lists.size () == 1) {
            // 如果只有一个列表，则直接返回它的元素作为单元素列表
            for (String item : lists.get ( 0 )) {
                result.add ( Collections.singletonList ( item ) );
            }
        } else {
            // 递归地计算除了第一个列表之外的笛卡尔积
            List<List<String>> restCartesian = cartesianProduct ( lists.subList ( 1, lists.size () ) );
            // 将第一个列表中的每个元素与剩余笛卡尔积的每个列表组合起来
            for (String first : lists.get ( 0 )) {
                for (List<String> rest : restCartesian) {
                    List<String> combination = new ArrayList<> ( rest.size () + 1 );
                    combination.add ( first );
                    combination.addAll ( rest );
                    result.add ( combination );
                }
            }
        }
        return result;
    }

    @ApiOperation ( "添加表单教验" )
    @PostMapping("GoodsInfoadd")
    public R add(@RequestBody GoodsInfoDTO GoodsInfoDTO){
        return R.ok ();
    }



    @ApiOperation ( "sku相关属性属性" )
    @GetMapping("skutype")
    public R<List<ShopSkuStockVO>> skutype(@RequestParam Integer id){
        List<ShopSkuStockVO> list=pmsBrandService.skutype(id);
        return R.ok (list);
    }

}
